package com.example.back.mapper;

import com.example.back.model.Contain;
import com.example.back.model.Order;
import com.example.back.model.OrderProduct;
import com.example.back.model.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMapper {
    @Select("select * from the_order")
    List<Order> findAllOrder();
    @Select("select * from the_order where account=#{account} order by submitTime desc")
    List<Order> findOrder(String account);
    @Select("select * from product left join contain on product.id = contain.productID where orderID=#{orderID}")
    List<OrderProduct> findOrderProduct(int orderID);
    @Select("select * from product left join contain on product.id = contain.productID where orderID=#{orderID} and type=#{type}")
    List<OrderProduct> findOrderProductByType(int orderID, String type);
    @Select("select * from product left join contain on product.id = contain.productID where orderID=#{orderID} and instr(name, #{name})>0")
    List<OrderProduct> findOrderProductByName(int orderID, String name);
    @Select("select state from the_order where orderID=#{orderID}")
    int findOrderState(int orderID);
    @Select("select * from product left join contain on product.id = contain.productID where orderID=#{orderID} and productID=#{productID}")
    OrderProduct findTheProduct(int orderID, int productID);
//    @Select("select * from product left join contain on product.id = contain.productID where orderID=#{orderID}")
//    List<Product> findTheOrderProduct(int orderID);
    @Insert("insert into the_order(account, room, submitTime, state, cost) values(#{account}, #{room}, #{submitTime}, 0, 0)")
    Boolean addOrder(String account, String room, String submitTime);
    @Select("select orderID from the_order where account=#{account} and room=#{room} and state=0")
    Order findOrderID(String account, String room);
    @Insert("insert into contain(orderID, productID, count, cost) values(#{orderID}, #{productID}, #{count}, #{cost})")
    Boolean addProduct(int orderID, int productID, int count, double cost);
    @Delete("delete from contain where orderID=#{orderID} and productID=#{productID}")
    Boolean removeProduct(int orderID, int productID);
    @Update("update the_order set cost=cost+#{cost} where orderID=#{orderID}")
    Boolean addCost(int orderID, double cost);
    @Select("select * from contain where orderID=#{orderID} and productID=#{productID}")
    Contain findProduct(int orderID, int productID);
    @Update("update contain set count=count+#{count}, cost=cost+#{cost} where orderID=#{orderID} and productID=#{productID}")
    Boolean updateProduct(int orderID, int productID, int count, double cost);
    @Select("select * from the_order where state=#{state}")
    List<Order> findOrderByState(int state);
    @Select("select * from the_order where account=#{account}")
    List<Order> findOrderByAccount(String account);
    @Select("select * from the_order where room=#{room}")
    List<Order> findOrderByRoom(String room);
    @Delete("delete from the_order where orderID=#{orderID}")
    Boolean deleteOrder(int orderID);
    @Update("update the_order set state=1 where orderID=#{orderID}")
    Boolean finishOrder(int orderID);
}
